package org.gruposp2p.aularest.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 *
 * @author jj
 */
public class DateUtils {

    private static final Logger logger =
            Logger.getLogger(DateUtils.class.getName());

    public static int getDayOfMonthFromDate (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static int getMonthFromDate (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH);
    }

    public static Date getDateForAulaCalendar (int day, int month) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        calendar.set(year, month, day);
        return calendar.getTime();
    }


    public static boolean isValidDate (int date, int month)
        throws ParseException {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        return isValidDate(date, month, year);
    }

    public static boolean isValidDate (int date, int month, int year) {
        boolean result = false;
        /*
        String strDate = getFixedLengthString(date) + "/"
                + getFixedLengthString(month) + "/"
                + String.valueOf(year);*/
        String strDate = String.valueOf(date) + "/"
                + String.valueOf(month) + "/"
                + String.valueOf(year);
        DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
        dateFormat.setLenient(false);
        try {
            Date date1 = dateFormat.parse(strDate);
        } catch (ParseException ex) {
            return false;
        }
        result = true;
        return result;
    }

    private static String getFixedLengthString (int number) {
        String result = String.valueOf(number);
        result = (result.length() == 1)?  "0" + result : result;
        return result;
    }

    public static Date getTodayDate () {
        return new Date(System.currentTimeMillis());
    }

    public static int getHourFromDate (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static DateFormat getJXDatePickerFormatter () {
        DateFormat dateFormat = new SimpleDateFormat ("E, dd MMM yyyy");
        return dateFormat;
    }

    public static XMLGregorianCalendar getXMLGregorianCalendar (Date date) {
        Calendar gcalendar = new GregorianCalendar();
        gcalendar.setTime(date);
        XMLGregorianCalendar xmlDate = null;
        try {
            DatatypeFactory df = DatatypeFactory.newInstance();
            xmlDate =    df.newXMLGregorianCalendarDate(
                    gcalendar.get(Calendar.YEAR),
                    gcalendar.get(Calendar.MONTH) + 1,
                    gcalendar.get(Calendar.DAY_OF_MONTH),
                    DatatypeConstants.FIELD_UNDEFINED );
        } catch (DatatypeConfigurationException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return xmlDate;
    }

}
